package br.com.easyrhour3x.main.cadastrar;

import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;

import br.com.easyrhour3x.controller.UsuarioController;
import br.com.easyrhour3x.hDAO.GenericDAO;
import br.com.easyrhour3x.main.ConfigUIPadrao;
import br.com.easyrhour3x.model.Usuario;
import br.com.easyrhour3x.util.Teclas;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class UIListarUsuario extends ConfigUIPadrao {

	private static final long serialVersionUID = 1L;
	private UsuarioController usuarioController = new UsuarioController();
	private JPanel contentPane;
	private JTable table;
	private JFrame jframeLocal;

	private void setDadosUsuario(int row) {
		GenericDAO<Usuario, Integer> dao = new GenericDAO<Usuario, Integer>(Usuario.class);
		UIUsuario jframe = (UIUsuario) jframeLocal;
		Usuario usuario = (Usuario) dao.consulta(Integer.parseInt(table.getValueAt(row, 0)
				.toString()));
		jframe.setDadosUsuario(usuario);
		sair();
	}

	public UIListarUsuario(JFrame jframe) {
		jframeLocal = jframe;
		setAlwaysOnTop(true);
		setResizable(false);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				sair();
			}
		});
		setTitle("Lista de Usu\u00E1rios");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(331, 205);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
		setContentPane(contentPane);

		table = new JTable();
		table.setFont(new Font("Tahoma", Font.PLAIN, 14));
		table.setAutoCreateRowSorter(true);
		table.getTableHeader().setReorderingAllowed(false);

		UsuarioTableModel model = new UsuarioTableModel(usuarioController.getListaUsuario(), 2);
		table.setModel(model);
		table.getColumnModel().getColumn(0).setPreferredWidth(25);
		table.getColumnModel().getColumn(1).setPreferredWidth(255);
		table.changeSelection(0, 2, false, false);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				if (arg0.getButton() == MouseEvent.BUTTON1) {
					setDadosUsuario(table.rowAtPoint(arg0.getPoint()));
				}
			}
		});
		table.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if (arg0.getKeyCode() == Teclas.ENTER.getValorTecla()) {
					setDadosUsuario(table.getSelectedRow());
				}
			}
		});
		JScrollPane scroll = new JScrollPane(table);

		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(gl_contentPane
				.createParallelGroup(Alignment.LEADING).addComponent(scroll,
						GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE));
		gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addComponent(scroll, GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE));
		contentPane.setLayout(gl_contentPane);
	}

	@Override
	public void sair() {
		UIUsuario.uiListarUsuario = null;
		dispose();

	}

}
